﻿layui.config({
    version: true,
    base: '/Plugin/'
}).extend({ //设定模块别名
    md5: 'md5', //如果 mymod.js 是在根目录，也可以不用设定别名
    common: 'common' //相对于上述 base 目录的子目录
    }).use(['element', 'jquery', 'layer', 'table', 'form', 'util', 'common'], function () {
    var $ = layui.$,
        form = layui.form,
        util = layui.util,
        table = layui.table,
        layer = layui.layer,
        common = layui.common,
        layedit = layui.layedit,
        jhy = layui.data("jhy");
    var editIntro = null;
    var editContent = null;
    //必须在框架内使用
    common.noneIframe();

    //初始化表格
    var tableArticle = table.render({
        elem: '#tableArticle',
        url: '/Ashx/article.ashx', //数据接口
        method: 'post',
        where: { functionname: 'jhy_article_query', art_userid: jhy.user_id, art_cateid: "", art_title: "" },
        page: true,
        cols: [[ //表头 
            {
                field: 'art_title', title: '文章标题', minWidth: 300, templet: function (d) {
                    return "<a href=\"/details.html?art_id=" + d.art_id + "\" target='_blank'>" + d.art_title + "</a>"
                }
            },
            { field: 'art_catename', title: '文章类别', width: 100, align: "center" },
            {
                field: 'art_state', title: '状态', width: 60, align: "center"
                , templet: function (d) {
                    if (d.art_state == 0) {
                        return '发布'
                    } else {
                        return '草稿'
                    }
                }
            },
            { field: 'art_username', title: '发布人', width: 80, align: "center" },
            {
                field: 'art_datetime', title: '发布日期', width: 120, align: "center"
                , templet: function (d) {
                    return util.timeAgo(d.art_datetime, true)
                }
            },
            { title: "操作", templet: "#toolbarDone", width: 100, align: "center", fixed: "right" }
        ]], done: function () {
            $("#tableArticle").next("div").find(".layui-table-tool").find(".layui-form-item").css("margin-bottom", "0px");
        }
    });

    //显示文章分类
    $.ajax({
        type: "post",
        url: "/Ashx/article.ashx",
        data: { functionname: "jhy_category_select_query" },
        success: function (msg) {
            $("#category").html(msg);
            $("#art_category").html(msg);
            $("#art_category").children("option[value='']").remove();
            form.render("select")
        }
    });

    //查询按钮操作
    form.on("submit(search)", function (data) {
        var data = data.field;
        var art_userid = jhy.user_id;
        var art_cateid = data.category;
        var art_title = data.art_title;
        tableArticle.reload({
            where: { functionname: 'jhy_article_query', art_userid: art_userid, art_cateid: art_cateid, art_title: art_title },
        })
        return false;
    })

    //删除、修改按钮操作
    table.on('tool(tableArticle)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        var tr = obj.tr; //获得当前行 tr 的DOM对象
        if (layEvent === 'delete') { //删除
            layer.confirm('真的删除【' + data.art_title + '】?', { icon: 3, title: "删除提示" }, function (index) {
                $.ajax({
                    type: "post",
                    url: "/Ashx/article.ashx",
                    data: { functionname: "jhy_article_delete", art_id: data.art_id },
                    success: function (msg) {
                        if (msg == 'true') {
                            obj.del();
                            layer.msg("成功");
                        } else {
                            layer.msg("失败");
                            tableArticle.reload();
                        }
                        layer.close(index);
                    }
                });
            });
        } else if (layEvent === 'edit') { //编辑
            parent.layui.layer.open({
                title: "修改",
                type: 1,
                area: ["100%", "100%"],
                skin: 'jhy-layer-css noleft',
                content: $("#layerArticle").html(),
                btn: ['发布'],
                success: function () {
                    //初始化数据
                    parent.layui.$("#art_id").val(data.art_id);
                    parent.layui.$("#art_title").val(data.art_title);
                    parent.layui.$("#art_category").val(data.art_cateid);
                    if (data.art_state != "0") {
                        parent.layui.$("#art_state").prop("checked", true);
                    }
                    parent.layui.$("#art_intro").val(unescape(data.art_intro));
                    parent.layui.$("#art_content").val(unescape(data.art_content));
                    parent.layui.form.render();
                    editIntro = parent.layui.layedit.build('art_intro',
                        {
                            height: 100
                        }); //建立编辑器
                    editContent = parent.layui.layedit.build('art_content',
                        {
                            uploadImage: {
                                url: '/Ashx/uploadImg.ashx',
                                type: 'post'
                            },
                            tool: [
                                'strong' //加粗
                                , 'italic' //斜体
                                , 'underline' //下划线
                                , 'del' //删除线

                                , '|' //分割线

                                , 'left' //左对齐
                                , 'center' //居中对齐
                                , 'right' //右对齐
                                , 'link' //超链接
                                , 'unlink' //清除链接
                                , 'face' //表情
                                , 'image' //插入图片  
                                , 'code'
                            ]
                        }); //建立编辑器
                }, yes: function (index, layero) {
                    parent.layui.$("#art_post").click();

                }
            });
        }
    });


    //发布文章
    $("#post").click(function () {
        parent.layui.layer.open({
            title: "文章发布",
            type: 1,
            area: ["100%", "100%"],
            skin: 'jhy-layer-css noleft',
            content: $("#layerArticle").html(),
            btn: ['发布'],
            success: function () {
                parent.layui.$("#art_id").val("");
                parent.layui.$("#art_title").val("");
                parent.layui.$("#art_category").prop("selectedIndex", 1);
                parent.layui.$("#art_state").prop("checked", false);
                parent.layui.$("#art_intro").val("");
                parent.layui.$("#art_content").val("");
                parent.layui.form.render();
                editIntro = parent.layui.layedit.build('art_intro',
                    {
                        height: 100
                    }); //建立编辑器
                editContent = parent.layui.layedit.build('art_content',
                    {
                        uploadImage: {
                            url: '/Ashx/uploadImg.ashx',
                            type: 'post'
                        },
                        tool: [
                            'strong' //加粗
                            , 'italic' //斜体
                            , 'underline' //下划线
                            , 'del' //删除线

                            , '|' //分割线

                            , 'left' //左对齐
                            , 'center' //居中对齐
                            , 'right' //右对齐
                            , 'link' //超链接
                            , 'unlink' //清除链接
                            , 'face' //表情
                            , 'image' //插入图片  
                            , 'code'
                        ]
                    }); //建立编辑器
            }, yes: function (index, layero) {
                parent.layui.$("#art_post").click();
            }
        });

    });

    parent.layui.form.on('submit(art_post)', function (data) {

        var data = data.field; //当前容器的全部表单字段，名值对形式：{name: value}
        var art_id = data.art_id;
        var art_title = data.art_title;
        var art_cateid = data.art_category;
        var art_state = data.art_state;
        art_state == "on" ? art_state = '1' : art_state = "0";
        var art_intro = escape(parent.layui.layedit.getContent(editIntro));
        var art_content = escape(parent.layui.layedit.getContent(editContent));

        if (art_id == "" || art_id == undefined) {
            $.ajax({
                type: "post",
                url: "/Ashx/article.ashx",
                data: { functionname: "jhy_article_post", art_title: art_title, art_cateid: art_cateid, art_state: art_state, art_intro: art_intro, art_content: art_content, art_userid: jhy.user_id },
                success: function (msg) {
                    if (msg == 'false') {
                        parent.layui.layer.msg("失败");
                        tableArticle.reload();
                    } else {
                        parent.layui.layer.msg("成功");
                        tableArticle.reload();
                    }
                    parent.layui.layer.closeAll('page');
                }
            });
        } else {
            $.ajax({
                type: "post",
                url: "/Ashx/article.ashx",
                data: { functionname: "jhy_article_edit", art_id: art_id, art_title: art_title, art_cateid: art_cateid, art_state: art_state, art_intro: art_intro, art_content: art_content },
                success: function (msg) {
                    if (msg == 'false') {
                        parent.layui.layer.msg("失败");
                        tableArticle.reload();
                    } else {
                        parent.layui.layer.msg("成功");
                        tableArticle.reload();
                    }
                    parent.layui.layer.closeAll('page');
                }
            });
        }






        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    });

});  
